# Maintainer: Alexey Pavlov <alexpux@gmail.com>

pkgbase=libffi
pkgname=('libffi' 'libffi-devel')
pkgver=3.2.1
pkgrel=3
pkgdesc="Portable, high level programming interface to various calling conventions"
arch=('i686' 'x86_64')
url="https://sourceware.org/libffi/"
groups=('libraries')
license=('MIT')
depends=()
checkdepends=('dejagnu')
#options=('!strip' 'debug')
install=libffi.install
source=(https://sourceware.org/pub/libffi/libffi-${pkgver}.tar.gz
        libffi-3.2.1-msysize.patch)
sha256sums=('d06ebb8e1d9a22d19e38d63fdb83954253f39bedc5d46232a05645685722ca37'
            'a700b3f7b77c39cb42a76581cdcb0d34b71daf20f9a222502b53f437c6f64800')

prepare() {
  cd ${pkgname}-${pkgver}
  patch -p1 -i ${srcdir}/libffi-3.2.1-msysize.patch

  autoreconf -fi
}

build() {
  cd ${pkgname}-${pkgver}
  # Perform the build outside of srcdir so that
  # srcdir is not simplified to '.' as relative
  # paths in debug information are not ideal.
  [[ -d build ]] && rm -rf build
  mkdir build
  cd build
  local -a extra_config
  if check_option "debug" "y"; then
    extra_config+=("--enable-debug")
  fi
  ${srcdir}/${pkgname}-${pkgver}/configure \
    --prefix=/usr \
    --srcdir=${srcdir}/${pkgname}-${pkgver} \
    --build=${CHOST} \
    --enable-static \
    --enable-shared \
    "${extra_config[@]}"
  make
  make DESTDIR="${srcdir}/dest" install
}

check() {
  cd ${pkgname}-${pkgver}/build
  make check
}

package_libffi() {
  mkdir -p ${pkgdir}/usr/bin
  cp -f ${srcdir}/dest/usr/bin/*.dll ${pkgdir}/usr/bin/
  cp -rf ${srcdir}/dest/usr/share ${pkgdir}/usr/
  install -Dm644 ${srcdir}/${pkgname}-${pkgver}/LICENSE "${pkgdir}"/usr/share/licenses/${pkgname}/LICENSE
}

package_libffi-devel() {
  pkgdesc="Libffi headers and libraries"
  groups=('development')
  if check_option "debug" "y"; then
    options=('staticlibs' '!strip' 'debug')
  else
    options=('staticlibs' '!strip' 'debug')
  fi
  depends=("libffi=${pkgver}")

  mkdir -p ${pkgdir}/usr
  #cp -rf ${srcdir}/dest/usr/include ${pkgdir}/usr/
  cp -rf ${srcdir}/dest/usr/lib ${pkgdir}/usr/
}
